/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.event;

import java.util.HashSet;
import java.util.Set;

import dgbgui.gui.widgets.Widget;
import dgbgui.gui.widgets.WidgetStore;
import dgbgui.gui.widgets.visual.QueryWidget;
import dgbgui.gui.widgets.visual.TitleBarWidget;
import dgbgui.gui.widgets.visual.modeldriven.ModelDrivenConceptWidget;
import dgbgui.gui.widgets.visual.results.ConceptronInstanceResultWidget;
import dgbgui.gui.widgets.visual.results.ConceptronTypeResultWidget;
import dgbgui.gui.widgets.visual.results.GraphResultWidget;
import dgbgui.modeldocument.Query;
import dgbgui.modeldocument.primitives.Conceptron;
import dgbgui.modeldocument.primitives.ConceptronType;
import dgbgui.modeldocument.primitives.Graph;
import dgbgui.selection.SelectionManager;
import edu.umd.cs.piccolo.event.PInputEvent;

public class SelectionConstructor {
    
    public static void setSelection(PInputEvent event, Set<Widget> widgets) {

        // Compute the selection from the widgets
        Class type = null;
        Set<Object> items = null;
        if ( widgets.size() > 0 ) {

            // Query widgets from the title bar (single click selection)
            if ( widgets.iterator().next() instanceof TitleBarWidget ) {
                Set<Object> queries = new HashSet<Object>();
                for ( Widget widget : widgets) {
                    queries.add(((TitleBarWidget)widget).getQuery());

                }
                items = queries;
                type = Query.class;
            }

            // Query widgets from query widgets (group selection)
            if ( widgets.iterator().next() instanceof QueryWidget ) {
                Set<Object> queries = new HashSet<Object>();
                for ( Widget widget : widgets) {
                    queries.add(WidgetStore.getInstance().getQuery((QueryWidget)widget));

                }
                items = queries;
                type = Query.class;
            }

            // Conceptron types
            if ( widgets.iterator().next() instanceof ConceptronTypeResultWidget ) {
                Set<Object> results = new HashSet<Object>();
                for ( Widget widget : widgets) {
                    results.add(((ConceptronTypeResultWidget)widget).getResult());
                }
                items = results;
                type = ConceptronType.class;
            }

            // Conceptron instances
            if ( widgets.iterator().next() instanceof ConceptronInstanceResultWidget ) {
                Set<Object> results = new HashSet<Object>();
                for ( Widget widget : widgets) {
                    results.add(((ConceptronInstanceResultWidget)widget).getResult());
                }
                items = results;
                type = Conceptron.class;
            }
            

            // Concept instances from ModelDrivenConceptWidget 
            if ( widgets.iterator().next() instanceof ModelDrivenConceptWidget ) {
                Set<Object> results = new HashSet<Object>();
                for ( Widget widget : widgets) {
                    results.add(((ModelDrivenConceptWidget)widget).getConcept());
                }
                items = results;
                type = Conceptron.class;
            }

            // Graph instances
            if ( widgets.iterator().next() instanceof GraphResultWidget ) {
                Set<Object> results = new HashSet<Object>();
                for ( Widget widget : widgets) {
                    results.add(((GraphResultWidget)widget).getResult());
                }
                items = results;
                type = Graph.class;
            }


        }

        if ( ( type != null ) && ( items != null ) ) {
            if ( event.isShiftDown() ) {
                SelectionManager.getInstance().xorSet(type,items);
            } else {
                SelectionManager.getInstance().setSet(type,items);
            }
        }  else {
            if ( !event.isShiftDown() ) {
                SelectionManager.getInstance().clear();
            }
        }
    }

    
}
